package neuedu;


/**
 * @description:
 * @author: Peng Jiwei
 * @create :2021/10/8-09:30
 */
public class T1 {
    public static void main(String[] args) {
        int nums1[] = {2,2,4,4};
        int nums2[] = {2,2,4,4};
        System.out.println(findMedianSortedArrays(nums1,nums2));
    }

    public static double findMedianSortedArrays(int[] nums1, int[] nums2) {
        int n1=0;  //找到中位数所在的位置
        int n2=0;
        int x = nums1.length+nums2.length;
        if(x%2!=0){
            n1 = n2 = (x+1)/2;
            n2 = n1;
        }
        else{
            n1 = x/2;
            n2 = n1+1;
        }
        int point1 = 0; //在数组1遍历指针；
        int point2 = 0; //在数组2遍历的指针；

        int a = 0;  //两个中位数
        int b = 0;

        for(int i=0;i<x;i++){
            int number = 0;
            if(point1<nums1.length && point2<nums2.length){
                if(nums1[point1]<=nums2[point2]){
                    number = nums1[point1];
                    point1++;
                }
                else {
                    number = nums2[point2];
                    point2++;
                }
            }
            else if(point1 == nums1.length){
                number = nums2[point2];
                point2++;
            }
            else {
                number = nums1[point1];
                point1++;
            }

            if((i+1) == n1){
                a = number;
            }
            if((i+1) == n2){
                b = number;
                break;
            }
        }
        return  (a+b+0.0)/2;
    }
}
